﻿//AcademicPlanner - course registration planning web app.
//Copyright (C) 2009  Boubacar Diallo

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


using System;

namespace AcademicPlanner.Domain
{
	/// <summary>
	/// Task is the base class for all data changing operations in the
	/// AcademicPlanner application's domain layer. A Task is a way of
	/// atomitising a data changing operation by adding some transaction
	/// support to it.
	/// </summary>
	/// <version>1.0</version>
	/// <since>1.0</since>
	abstract class Task
	{
		/// <summary>
		/// Whether the Task succeeed without any errors or not.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		protected bool _succeeded;


		/// <summary>
		/// Gets the <c>DomainMaster</c> of the AcademicPlanner application.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		protected DomainMaster DomainMaster { get { return DomainMaster.GetInstance(); } }


		/// <summary>
		/// Runs a Task. The Task is initialized, performed and completed.
		/// If no exception is thrown during the running of the Task, the
		/// <c>_succeeded</c> field is set to <c>true</c>.
		/// </summary>
		/// <seealso cref="M:Init"/>
		/// <seealso cref="M:Perform"/>
		/// <seealso cref="M:Complete"/>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public void Run()
		{
			try
			{
				Init();
				Perform();
				_succeeded = true;
			}
			finally
			{
				Complete();
			}
		}

		/// <summary>
		/// Initializes this Task by getting the data layer ready for transaction
		/// oriented operations.
		/// </summary>
		/// <seealso cref="P:DomainMaster.DSAS"/>
		/// <seealso cref="M:AcademicPlanner.Data.DataSourceAccessSupervisor.PrepareForWork"/>
		/// <version>1.0</version>
		/// <since>1.0</since>
		protected virtual void Init()
		{
			DomainMaster.DSAS.PrepareForWork();
		}

		/// <summary>
		/// Completes this Task by getting the data layer clean up after transaction
		/// oriented operations based on <c>_succeeded</c> field.
		/// </summary>
		/// <seealso cref="P:DomainMaster.DSAS"/>
		/// <seealso cref="M:AcademicPlanner.Data.DataSourceAccessSupervisor.CleanUpAfterWork"/>
		/// <version>1.0</version>
		/// <since>1.0</since>
		protected virtual void Complete()
		{
			DomainMaster.DSAS.CleanUpAfterWork(_succeeded);
			if (_succeeded)
				Succeeded();
			else
				Failed();
		}

		/// <summary>
		/// Performs this Task's bulk work. This method is abstract.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		protected abstract void Perform();

		protected virtual void Succeeded() { }

		protected virtual void Failed() { }
	}
}
